package cn.com.yusys.yusp.admin.service;

import cn.com.yusys.yusp.admin.repository.mapper.ProcessApprovalMapper;
import cn.com.yusys.yusp.commons.mapper.QueryModel;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
public class ProcessApprovalService {

    @Autowired
    ProcessApprovalMapper processApprovalMapper;

    @Transactional(readOnly = true)
    public List<Map<String, Object>> getApprovalInformation(QueryModel model) {
        PageHelper.startPage(model.getPage(), model.getSize());
        List<Map<String, Object>> list = processApprovalMapper.getApprovalInformation(model);
        PageHelper.clearPage();
        return list;
    }

    @Transactional(readOnly = true)
    public List<Map<String, Object>> getApprovalDetail(QueryModel model) {
        List<Map<String, Object>> list = processApprovalMapper.getApprovalDetail(model);
        return list;
    }

    @Transactional(readOnly = true)
    public List<Map<String, Object>> getMessageDetail(QueryModel model) {
        List<Map<String, Object>> list = processApprovalMapper.getMessageDetail(model);
        return list;
    }

    /**
     * 点击审批通过后，先更新该条审批的状态，再更新用户的地址，再更新另外一张表的状态
     * @param model
     * @return
     */
    @Transactional
    public String adoptProcess(QueryModel model) {
        Integer adoptProcess = processApprovalMapper.adoptProcess(model);
        Integer adoptTransfer = processApprovalMapper.adoptTransfer(model);
        Integer updateAddress = processApprovalMapper.updateAddress(model);
        if (adoptProcess == 1 && adoptTransfer==1 && updateAddress ==1){
            return "更新成功";
        }
        return "更新失败";
    }

    @Transactional
    public String refuseProcess(QueryModel model){
        Integer refuseProcess = processApprovalMapper.refuseProcess(model);
        Integer refuseTransfer = processApprovalMapper.refuseTransfer(model);
        if (refuseProcess == 1 && refuseTransfer==1){
            return "拒绝成功";
        }
        return "拒绝失败";
    }
}
